package 多线程.复习.线程方法;

import 多线程.复习.线程安全.SynchronizedKey;

public class NotifyAndWait {
    private static class ThreadOfOB{
        static Object lock = new Object();
    }

    public static void main(String[] args) {
        ThreadOfOB ob = new ThreadOfOB();
        Thread t1 = new Thread(()->{
            synchronized(ob.lock) {
                try {
                    System.out.println(Thread.currentThread().getName()+"线程进入等待状态");
                    ob.lock.wait();
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
                System.out.println(Thread.currentThread().getName()+"线程被唤醒");
                ob.lock.notify();
            }
        });
        Thread t2 = new Thread(() ->{
            synchronized(ob.lock) {
                try {
                    System.out.println(Thread.currentThread().getName()+"线程进入等待状态");
                    ob.lock.wait();
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
                System.out.println(Thread.currentThread().getName()+"线程被唤醒");

            }
        });
        Thread t3 = new Thread(() ->{
            synchronized(ob.lock) {
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
                System.out.println(Thread.currentThread().getName()+"准备唤醒锁");
                ob.lock.notify();
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
                System.out.println(Thread.currentThread().getName()+"唤醒了lock锁");
            }
        });
        t1.start();
        t2.start();
        t3.start();
    }
}
